package com.yswee.mallnavi.activity.base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.yswee.framework.widget.loading.BlankView;
import com.yswee.framework.widget.loading.ErrorView;
import com.yswee.framework.widget.loading.LoadingView;

public abstract class LoadingActivity extends BaseActivity {
	
	public static final int NOTSET = 0;
	public static final int INITIALIZED = 1;
	public static final int LOADING = 2;
	public static final int SUCCESSFUL = 4;
	public static final int ERROR = 5;
	
	protected int mStatus = NOTSET;
	
	protected View BlankView;
	protected View LoadingView;
	protected View ErrorView;
	protected View ContentView;
	
	public int getStatus() {
		return mStatus;
	}
	
	protected boolean getAutoLoading() {
		return false;
	}
	
	protected boolean getRetryOnError() {
		return true;
	}

	public LoadingActivity() { }
	
	@Override
	public void setContentView(int layoutResID) {
		View view = LayoutInflater.from(this).inflate(layoutResID, null, false);
		View contentView = getContentView(view);
		transferContentView(contentView);
		super.setContentView(contentView);
	}
	
	@Override
	public void setContentView(View view) {
		View contentView = getContentView(view);
		transferContentView(contentView);
		super.setContentView(contentView);
	}
	
	@Override
	public void setContentView(View view, LayoutParams params) {
		View contentView = getContentView(view);
		transferContentView(contentView);
		super.setContentView(contentView, params);
	}
	
	private View getContentView(View view) {
		RelativeLayout root = new RelativeLayout(getBaseContext());
		if (view.getBackground() != null) {
			root.setBackground(view.getBackground());
			view.setBackground(null);
		}
		root.addView(ContentView = view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		int blankResId = getBlankResId();
		if(blankResId != 0) {
			BlankView = LayoutInflater.from(this).inflate(blankResId, null, false);
		}
		if(BlankView == null) {
			BlankView = getBlankView();
		}
		if(BlankView != null) {
			root.addView(BlankView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			BlankView.setVisibility(View.GONE);
		}
		int errResId = getErrorResId();
		if(errResId != 0) {
			ErrorView = LayoutInflater.from(this).inflate(errResId, null, false);
		}
		if(ErrorView == null) {
			ErrorView = getErrorView();
		}
		if(ErrorView != null) {
			root.addView(ErrorView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			ErrorView.setVisibility(View.GONE);
		}
		int loadingResId = getLoadingResId();
		if(loadingResId != 0) {
			LoadingView = LayoutInflater.from(this).inflate(loadingResId, null, false);
		}
		if(LoadingView == null) {
			LoadingView = getLoadingView();
		}
		if(LoadingView != null) {
			root.addView(LoadingView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			LoadingView.setVisibility(View.GONE);
		} else {
			throw new IllegalStateException("you should override getLoadingResId or getLoadingView method");
		}
		mStatus = INITIALIZED;
		return root;
	}

	protected void transferContentView(View v) {

	}
	
	@Override
	protected void onBindListener() {
		super.onBindListener();
		if(BlankView != null) {
			BlankView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onApplyLoadingData();
				}
			});
		}
		if(ErrorView != null) {
			ErrorView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onApplyLoadingData();
				}
			});
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(getAutoLoading() && (mStatus == INITIALIZED || 
				(mStatus == ERROR && getRetryOnError()))) {
			onApplyLoadingData();
		}
	}
	
	/**
	 * 显示loading时是否隐藏contentview
	 * @return
	 */
	protected boolean hideContentOnAction() {
		return false;
	}
	
	/**
	 * 切换至加载空白状态
	 */
	public void gotoBlank() {
		if(LoadingView != null)
			LoadingView.setVisibility(View.GONE);
		if(ContentView != null)
			ContentView.setVisibility(View.GONE);
		if(ErrorView != null)
			ErrorView.setVisibility(View.GONE);
		if(BlankView != null)
			BlankView.setVisibility(View.VISIBLE);
		mStatus = SUCCESSFUL;
	}
	
	/**
	 * 切换至加载中状态
	 */
	public void gotoLoading() {
		if(mStatus == LOADING)
			return;
		if(ContentView != null)
			ContentView.setVisibility(hideContentOnAction() ? View.GONE : View.VISIBLE);
		if(BlankView != null)
			BlankView.setVisibility(View.GONE);
		if(ErrorView != null)
			ErrorView.setVisibility(View.GONE);
		if(LoadingView != null)
			LoadingView.setVisibility(View.VISIBLE);
		mStatus = LOADING;
	}
	
	/**
	 * 切换至加载出错状态
	 */
	public void gotoError() {
		if(mStatus == ERROR)
			return;
		if(LoadingView != null)
			LoadingView.setVisibility(View.GONE);
		if(ContentView != null)
			ContentView.setVisibility(View.GONE);
		if(BlankView != null)
			BlankView.setVisibility(View.GONE);
		if(ErrorView != null)
			ErrorView.setVisibility(View.VISIBLE);
		mStatus = ERROR;
	}
	
	/**
	 * 切换至加载成功状态
	 */
	public void gotoSuccessful() {
		if(LoadingView != null)
			LoadingView.setVisibility(View.GONE);
		if(BlankView != null)
			BlankView.setVisibility(View.GONE);
		if(ErrorView != null)
			ErrorView.setVisibility(View.GONE);
		if(ContentView != null)
			ContentView.setVisibility(View.VISIBLE);
		mStatus = SUCCESSFUL;
	}
	
	/**
	 * 加载数据
	 */
	public void onApplyLoadingData() {
		gotoLoading();
	}
	
	/**
	 * 获取加载中布局文件resid
	 * @return
	 */
	protected int getLoadingResId() {
		return 0;
	}
	/**
	 * 获取加载中布局文件resid
	 * @return
	 */
	protected View getLoadingView() {
		return new LoadingView(this);
	}
	/**
	 * 获取加载出错布局文件resid
	 * @return
	 */
	protected int getErrorResId() {
		return 0;
	}
	/**
	 * 获取加载出错布局文件resid
	 * @return
	 */
	protected View getErrorView() {
		return new ErrorView(this);
	}
	/**
	 * 获取加载无数据布局文件resid
	 * @return
	 */
	protected int getBlankResId() {
		return 0;
	}
	/**
	 * 获取加载无数据布局文件resid
	 * @return
	 */
	protected View getBlankView() {
		return new BlankView(this);
	}
	

}